﻿using System.Collections.Generic;
using System.Globalization;
using System.Xml.Linq;
using Engine;
using XmlUtilities;

namespace Game
{
    public static class MoneyManager
    {
        public static List<Money> m_recipes = new List<Money>();

        public static int CoinValue => 987;

        public static string CoinChar => GetBlockDisplayName(CoinValue);

        public static void Initialize()
        {
            foreach (XElement item in ContentManager.Get<XElement>("Money").Descendants("Money"))
            {
                Money money = new Money();
                string attributeValue = XmlUtils.GetAttributeValue<string>(item, "Result");
                money.ResultValue = DecodeResult(attributeValue);
                money.itemMoney = XmlUtils.GetAttributeValue<int>(item, "Money");
                money.returnMoney = XmlUtils.GetAttributeValue<int>(item, "ReturnMoney");
                m_recipes.Add(money);
            }
        }

        public static int DecodeResult(string result)
        {
            string[] array = result.Split(':');
            Block block = BlocksManager.FindBlockByTypeName(array[0], throwIfNotFound: true);
            int data = ((array.Length >= 2) ? int.Parse(array[1], CultureInfo.InvariantCulture) : 0);
            return Terrain.MakeBlockValue(block.BlockIndex, 0, data);
        }

        public static int GetItemMoney(int value)
        {
            foreach (Money recipe in m_recipes)
            {
                if (recipe.ResultValue == value)
                {
                    return recipe.itemMoney;
                }
            }

            return -1;
        }

        public static int GetReturnMoney(int value)
        {
            foreach (Money recipe in m_recipes)
            {
                if (recipe.ResultValue == value)
                {
                    return recipe.returnMoney;
                }
            }

            return -1;
        }

        public static string GetBlockDisplayName(int value)
        {
            int num = Terrain.ExtractContents(value);
            return BlocksManager.Blocks[num].GetDisplayName(null, value);
        }

        public static int GetItemCountInInventory(int value, IInventory inventory)
        {
            int num = 0;
            for (int i = 0; i < inventory.SlotsCount; i++)
            {
                if (inventory.GetSlotValue(i) == value)
                {
                    num += inventory.GetSlotCount(i);
                }
            }

            return num;
        }

        public static void AddItem(ComponentMiner componentMiner, SubsystemPickables subsystemPickables, int value, int count)
        {
            IInventory inventory = componentMiner.Inventory;
            for (int i = 0; i < count; i++)
            {
                if (ComponentInventoryBase.FindAcquireSlotForItem(inventory, value) >= 0)
                {
                    ComponentInventoryBase.AcquireItems(inventory, value, 1);
                }
                else
                {
                    subsystemPickables.AddPickable(value, 1, componentMiner.ComponentCreature.ComponentBody.Position + new Vector3(0f, 1f, 0f), null, null);
                }
            }
        }

        public static void RemoveItemFromInventory(int value, int count, IInventory inventory)
        {
            for (int i = 0; i < inventory.SlotsCount; i++)
            {
                if (count <= 0)
                {
                    break;
                }

                if (inventory.GetSlotValue(i) == value)
                {
                    int num = MathUtils.Min(count, inventory.GetSlotCount(i));
                    count -= num;
                    inventory.RemoveSlotItems(i, num);
                }
            }
        }
    }
}